﻿using System;
using Chip8.Core.Bus;

namespace Chip8.Core.Processor.Operations
{
	internal class SUBN : Operation
	{
		public SUBN(int targetRegisterIndex, int sourceRegisterIndex)
		{
			SourceRegisterIndex = sourceRegisterIndex;
			TargetRegisterIndex = targetRegisterIndex;
		}

		public int SourceRegisterIndex { get; set; }

		public int TargetRegisterIndex { get; set; }

		public override void Execute(ICPU cpu)
		{
			var result = cpu.V[SourceRegisterIndex] - cpu.V[TargetRegisterIndex];
			cpu.Carry = result >= 0 ? (byte)1 : (byte)0;
			unchecked
			{
				cpu.V[TargetRegisterIndex] = (byte)result;
			}
		}
	}
}